{include file="public/toper" /}
<style>
    .layui-table-cell {
        font-size:14px;
        padding:0 5px;
        height:auto;
        overflow:visible;
        text-overflow:inherit;
        white-space:normal;
        word-break: break-all;
    }
    .layui-table-fixed {display: none;}
</style>
<div class="x-nav">
    <span class="layui-breadcrumb">
        <a href="">结算中心</a>
        <a><cite>申请结算</cite></a>
      </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
</div>
<div class="x-body">
    <div class="layui-row">
        <div class="layui-form layui-col-md12 x-so">
            <input type="text" name="sreach" value=""  style="width:250px;" id="key"   placeholder="订单号/收货人地址电话名称" autocomplete="off" class="layui-input">
            <button class="layui-btn" id="search"><i class="layui-icon">&#xe615;</i></button>
        </div>
    </div>
    <xblock class="demoTable">
        <button class="layui-btn" data-type="getCheckData">申请结算</button>
    </xblock>
    <table class="layui-hide" id="test" lay-filter="test"></table>
</div>
<script type="text/html" id="sn">
    <div><a href="#"  onclick="x_admin_show('查看订单','{:url(\'admin/Order/order_details\')}?id={{d.order_id}}')" title="查看详情" class="layui-table-link">{{d.order_sn}}</a></div>
    <div>{{#  if(d.type==1){ }}会员{{#  } else if(d.type==2) { }}销售{{#  } }}</div>
</script>
<script type="text/html" id="goods">
    {{#  layui.each(d.goods, function(index, item){ }}
    <div>{{ item.gname}}        *   {{ item.goods_number}}</div>
    {{#  if(d.type==1){ }}
    <div>周期：{{d.lease_end_time}}   日租：{{ item.settlement}}</div>
    {{#  } }}
    {{#  }); }}
</script>
<script type="text/html" id="xs">
    {{#  layui.each(d.goods, function(index, item){ }}
    <div class="color">{{ item.goods_price}}</div>
    {{#  if(d.type==1){ }}
    <div>逾期：{{d.overdue_price}}</div>
    <div>磨损：{{d.abrasion_price}}</div>
    {{#  } }}
    {{#  }); }}
</script>
<script type="text/html" id="js">
    {{#  layui.each(d.goods, function(index, item){ }}
    {{#  if(d.type==1){ }}
    <div class="color">{{ d.s_price}}</div>
    <div>租金：{{d.lease_end_time * d.business_settlement_price}}</div>
    <div>逾期：{{d.overdue_price}}</div>
    <div>磨损：{{d.abrasion_price}}</div>
    {{#  } else if(d.type==2) { }}
    <div class="color">{{ d.business_settlement_price}}</div>
    {{#  } }}
    {{#  }); }}
</script>
<script type="text/html" id="sj">
    <div>{{layui.util.toDateString(d.add_time*1000)}}</div>
    <div>{{layui.util.toDateString(d.end_time*1000)}}</div>
</script>
<script>
    layui.use(['table','util','layer'], function(){
        var table = layui.table, $ = layui.jquery;
        var layer = layui.layer;
        var loading = layer.load(1, {shade: [0.1,'#fff'] });
        var tableIn = table.render({
            method: 'post',
            elem: '#test'
            ,url: "{:url('Sett/index')}"
            ,id:'test'
            ,cols: [[
                {type:'checkbox'}
                ,{field:'order_id', width:80, title: '序号',align: 'center',type:'numbers'}
                ,{width:190, title: '订单编号',align: 'center',templet:'#sn'}
                ,{width: 200,title: '商品信息', align: 'center',templet:'#goods'}
                ,{width: 200,title: '销售价格', align: 'center',templet:'#xs'}
                ,{width: 200,title: '结算价格', align: 'center',templet:'#js'}
                ,{width:190, title: '下单时间/完结时间',align: 'center',templet:'#sj'}
                ,{field:'settlement', width:100, title: '状态',align: 'center',templet: function(d){if(d.settlement==0){ return '未结算';}else if (d.settlement==1){ return '已结算';}}}
            ]]
            ,page: true
        });
        layer.close(loading);

        $('#search').on('click', function () {
            tableIn.reload({
                where: {key: $('#key').val()}
                ,page: {curr: 1 }
            });
        });


        var $ = layui.$, active = {
            getCheckData: function(){ //获取选中数据
                var checkStatus = table.checkStatus('test')
                    ,data = checkStatus.data;

                if(data.length>0){
                    var str = new Array();
                    var number = 0;
                    var prce = 0;
                    for(var i=0;i<data.length;i++){
                        str[i] = data[i].order_id;
                        number = number+1;
                        prce += data[i].s_price;
                    }
                    var loading = layer.load(1, {shade: [0.1,'#fff'] });
                    prce = prce.toFixed(2);
                    layer.open({
                        content: '结算订单数：'+number +',结算金额：'+prce
                        ,btn: ['确定', '取消']
                        ,btnAlign: 'c'
                        ,yes: function(index, layero){
                            $.post("{:url('Sett/settlement')}",{id:str,fees:prce},function(res){
                                layer.close(loading);
                                if(res.code==200){
                                    layer.alert(res.msg, {icon: 6},function () {
                                        layer.closeAll();
                                        tableIn.reload({});
                                    });

                                    // layer.msg(res.msg,{time:1000,icon:1});
                                    // tableIn.reload({});
                                }else{
                                    layer.msg(res.msg,{time:1000,icon:2});
                                }
                            });
                        }
                        ,btn2: function(index, layero){
                            layer.close(loading);
                            //按钮【按钮二】的回调
                            //return false 开启该代码可禁止点击该按钮关闭
                        }
                        ,cancel: function(){
                            layer.close(loading);
                            //右上角关闭回调
                            //return false 开启该代码可禁止点击该按钮关闭
                        }
                    });

                }else{
                    layer.msg("请选择要结算的订单",{time:1000,icon:2});
                }

            }
        };

        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });
</script>
{include file="public/footer"/}